package com.abc.robot01.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.ArrowDropUp
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.abc.robot01.R
import com.abc.robot01.vivemodel.YingZhuangViewModel


/**
 * 硬装
 */
class YingZhuangActivity : BaseActivity() {
    private val viewModel: YingZhuangViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.renderConfigId.value = intent.getStringExtra("renderConfigId")!!
        viewModel.load()
        val index = intent.getIntExtra("index", -1);
        if (index == 0) {
            viewModel.isExpanded.value = true
            viewModel.isExpanded2.value = false
            viewModel.isExpanded3.value = false
        } else
            if (index == 1) {
                viewModel.isExpanded.value = false
                viewModel.isExpanded2.value = true
                viewModel.isExpanded3.value = false
            } else
                if (index == 2) {
                    viewModel.isExpanded.value = false
                    viewModel.isExpanded2.value = false
                    viewModel.isExpanded3.value = true
                }
        setContent {
            MaterialTheme {
                Box {
                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
                    Row(modifier = Modifier.padding(30.px)) {
                        BackButton {
                            this@YingZhuangActivity.finish()
                        }
                        Spacer(modifier = Modifier.width(82.px))
                        Column {
                            BasicText(
                                text = "硬装",
                                style = TextStyle(
                                    fontSize = 30.mysp,
                                    color = MYBLACK,
                                    fontWeight = FontWeight.Bold
                                ),
                            )
                            Spacer(modifier = Modifier.height(30.px))
                            Column(
                                Modifier.verticalScroll(rememberScrollState()),
                                verticalArrangement = Arrangement.spacedBy(30.px)
                            ) {
                                Content("基础装修", isExpanded2 = viewModel.isExpanded) {
                                    Content01()
                                }

                                Content(
                                    "硬装主材",
                                    "￥123.89.00",
                                    isExpanded2 = viewModel.isExpanded2
                                ) {
                                    Column(
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .padding(top = 0.dp)
                                    ) {
                                        Content01()
                                    }
                                }

                                Content(
                                    "家居饰品",
                                    "￥123.89.00",
                                    isExpanded2 = viewModel.isExpanded3
                                ) {
                                    Column(
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .padding(top = 0.dp)
                                    ) {
                                        Content01()
                                    }
                                }


                            }

                        }
                    }
                    BottomBar2() {}
                }
            }
        }
        Log.e(TAG, "onCreate");
    }

    @OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
    @Composable
    fun ColumnScope.ItemRow(dataList: SnapshotStateList<Item>, item: Item) {
        ItemContent(item.name, item.size, item.price, item.total)
    }

    @Composable
    fun Content01() {
        Column(
            modifier = Modifier
                .fillMaxWidth()
//                .background(Color.Yellow)
                .padding(
                    start = 30.px,
                    end = 30.px,
                    top = 30.px
                )
        ) {
            Header2()
            val dataList = remember { dataList }
            dataList.forEach {
                ItemRow(dataList, it);
            }
//            LazyColumn {
//                items(dataList, key = { it.id }) { item ->
//
//                }
//            }
        }

    }

    @Composable
    fun Content(
        title: String,
        amount: String = "￥166,710.00",
        isExpanded2: MutableState<Boolean>,
        content: @Composable ColumnScope.() -> Unit
    ) {
        var isExpanded by remember { isExpanded2 }
        Card(
            modifier = Modifier
                .width(1400.px)
                .heightIn(min = 90.px)
                .background(Color.Transparent),
            shape = RoundedCornerShape(20.px)
        ) {

            Column(
                modifier = Modifier
                    .padding(top = 30.px, bottom = 30.px)
            )
            {

                Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.clickable {
                    isExpanded = isExpanded.not()
                }
//                        .background(Color.Red)
                ) {
                    Spacer(modifier = Modifier.width(50.px))
                    BasicText(
                        text = title,
                        style = TextStyle(
                            fontSize = MyFont_Size,
                            color = MYBLACK,
                            fontWeight = FontWeight.Bold
                        ),
                    )
                    Spacer(modifier = Modifier.weight(100f))
                    BasicText(
                        text = amount,
                        style = TextStyle(
                            fontSize = MyFont_Size,
                            color = Color.Blue,
                            fontWeight = FontWeight.Bold
                        ),
                    )
                    Spacer(modifier = Modifier.width(10.px))
                    Icon(
                        imageVector = if (isExpanded) Icons.Default.ArrowDropUp else Icons.Default.ArrowDropDown,
                        contentDescription = null,
                        modifier = Modifier
                            .size(48.px)
                    )
                    Spacer(modifier = Modifier.width(30.px))
                }
                AnimatedVisibility(visible = isExpanded) {
                    content()
                }
            }
        }
    }

    companion object {
        val TAG = "MainActivity"
    }

    @Composable
    fun ColumnScope.ItemContent(name: String, size: Int, price: String, total: Float) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .background(color = Color(0x00333333))
                .width(1340.px)
                .height(
                    62.px
                )
                .padding(start = 30.px, end = 30.px, top = 19.px, bottom = 19.px)
        ) {
            BasicText(
                modifier = Modifier.width(200.px),
                text = name + "0123",
                style = TextStyle(
//                          fontWeight = FontWeight.Bold,
                    fontSize = MyFont_Size,
                    color = Color.Black
                ),
            )

            Spacer(modifier = Modifier.width((220).px))

            BasicText(
                modifier = Modifier.width(100.px),
                text = "" + size + "",
                style = TextStyle(
                    fontSize = MyFont_Size,
                    color = Color.Black,
                    textAlign = TextAlign.End
                ),
            )

            Spacer(modifier = Modifier.width(190.px))

            BasicText(
                modifier = Modifier.width(150.px),
                text = price,
                style = TextStyle(
//                          fontWeight = FontWeight.Bold,
                    fontSize = MyFont_Size,
                    textAlign = TextAlign.End,
                    color = Color.Black
                ),
            )
            Spacer(modifier = Modifier.weight(100f))

            BasicText(
                text = "" + total + "",
                style = TextStyle(
                    fontSize = MyFont_Size,
                    color = Color.Black
                ),
            )
        }
    }

    @Composable
    fun ColumnScope.Header2() {
        val shape = RoundedCornerShape(
            topStart = 4.dp,
            topEnd = 4.dp,
            bottomStart = 0.dp,
            bottomEnd = 0.dp
        )
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .background(color = MYBLACK_Percent5, shape = shape)
                .width(1340.px)
                .height(
                    62.px
                )
                .padding(start = 30.px, end = 30.px, top = 19.px, bottom = 19.px)
        ) {
            BasicText(
                text = "名称",
                style = TextStyle(
//                          fontWeight = FontWeight.Bold,
                    fontSize = MyFont_Size,
                    color = Color.Black
                ),
            )
            Spacer(modifier = Modifier.width(372.px))

            BasicText(
                text = "套内面积(㎡)",
                style = TextStyle(
//                          fontWeight = FontWeight.Bold,
                    fontSize = MyFont_Size,
                    color = Color.Black
                ),
            )
            Spacer(modifier = Modifier.width(214.px))
            BasicText(
                text = "单价/单位",
                style = TextStyle(
//                          fontWeight = FontWeight.Bold,
                    fontSize = MyFont_Size,
                    color = Color.Black
                ),
            )
            Spacer(modifier = Modifier.weight(100f))
            BasicText(
                text = "金额(元)",
                style = TextStyle(
//                          fontWeight = FontWeight.Bold,
                    fontSize = MyFont_Size,
                    color = Color.Black
                ),
            )
        }
    }

    private val dataList = mutableStateListOf(
        Item(id = 1, name = "基础装修", price = "1229/㎡", size = 109, total = 13455.00f),
        Item(id = 2, name = "精装房", price = "1580/㎡", size = 90, total = 14220.00f),
        Item(id = 3, name = "豪华装修", price = "2999/㎡", size = 150, total = 485.00f),
        Item(id = 4, name = "现代简约", price = "14/㎡", size = 120, total = 17400.00f),
        Item(id = 5, name = "田园风格", price = "1350/㎡", size = 100, total = 13500.00f),
        Item(id = 6, name = "新中式风格", price = "2600/㎡", size = 130, total = 33800.00f),
        Item(id = 7, name = "北欧风格", price = "1750/㎡", size = 85, total = 14875.00f),
        Item(id = 8, name = "地中海风格", price = "2100/㎡", size = 140, total = 29400.00f),
        Item(id = 9, name = "新古典风格", price = "2500/㎡", size = 150, total = 37500.00f),
        Item(id = 10, name = "欧式风格", price = "1999/㎡", size = 95, total = 18990.00f)
    )

    data class Item(

        val id: Int = 0,
        /**
         * 名称
         */
        val name: String = "基础装修",

        /**
         * 价格
         */
        val price: String = "1229/㎡",
        /**
         *  面积
         */
        val size: Int = 109,

        /**
         * 金额
         */
        val total: Float = 13455.00f,
    )

}
